package com.hzw.saas.web.admin.news.controller;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.news.INewsService;
import com.hzw.saas.api.news.bo.NewsBO;
import com.hzw.saas.api.news.bo.NewsContentBO;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.web.admin.news.param.NewsImageParam;
import com.hzw.saas.web.admin.news.param.NewsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author EZ09
 * @since 07/06/2021
 */
@Controller
@Validated
@RequiredArgsConstructor
@Slf4j
@Api(tags = "新闻中心")
public class NewsController {

    private final INewsService newsService;

    @GetMapping("/news")
    @SysLog
    @ApiOperation(value = "获取新闻列表", notes = "分页获取新闻列表，按时间倒序排序")
    public ResponseEntity<IPage<NewsBO>> pageNews(PageParam page) {
        IPage<NewsBO> pageNews = newsService.pageNews2Admin(page.convert());
        return ResponseEntity.ok(pageNews);
    }

    @GetMapping("/news/{newsId}")
    @SysLog
    @ApiOperation(value = "获取新闻详情", notes = "获取新闻详情，含内容")
    public ResponseEntity<NewsContentBO> getNews(@PathVariable String newsId) {
        NewsContentBO newsContent = newsService.getNewsContent(newsId);
        AssertUtil.assertThrow("待查看的新闻不存在", Objects.isNull(newsContent));
        return ResponseEntity.ok(newsContent);
    }

    @PostMapping("/news")
    @SysLog
    @ApiOperation(value = "新增或更改新闻", notes = "新增或更改新闻")
    public ResponseEntity<Boolean> modifyNews(@RequestBody NewsParam newsParam) {
        NewsContentBO contentBO = MapperUtil.nf().map(newsParam, NewsContentBO.class);
        Boolean modifyNews = newsService.modifyNews(contentBO);
        return ResponseEntity.ok(modifyNews);
    }

    @DeleteMapping("/news/{pid}")
    @SysLog
    @ApiOperation(value = "删除新闻", notes = "删除新闻")
    public ResponseEntity<Boolean> modifyNews(@PathVariable @NotEmpty String pid) {
        Boolean deleteNews = newsService.deleteNews(pid);
        return ResponseEntity.ok(deleteNews);
    }

    @SysLog
    @ApiOperation(value = "提取微信封面", notes = "提取微信封面")
    @PostMapping("/news/titleImage")
    public ResponseEntity<String> titleImage(@RequestBody NewsImageParam newsImageParam) {
        String result;
        try {
            result = HttpUtil.get(newsImageParam.getWxUrl());
        } catch (Exception exception) {
            result = "";
        }
        String reg = "msg_cdn_url = \"(.*)\"";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(result);
        String titleImage = "";
        if (matcher.find()) {
            titleImage = matcher.group(1);
        }
        return ResponseEntity.ok(titleImage);
    }
}
